package com.youfan.control;

import com.youfan.entity.YoufanOrder;
import com.youfan.page.CommonPage;
import com.youfan.re.Result;
import com.youfan.service.YoufanOrderService;
import com.youfan.utils.CopyUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping(value = "order")
public class YoufanOrderControl {

    @Autowired
    private YoufanOrderService youfanOrderService;

    @RequestMapping(method = RequestMethod.POST,value = "add")
    @ResponseBody
    public Result add(@RequestBody YoufanOrder youfanOrder)
    {
        try {
            int sucessnums = youfanOrderService.insertOne(youfanOrder);
            return Result.success(sucessnums,"添加成功");
        }catch (Exception e){
            e.printStackTrace();
            return Result.fail("添加失败");
        }
    }

    @RequestMapping(method = RequestMethod.POST,value = "list")
    @ResponseBody
    public Result list(@RequestBody YoufanOrder youfanOrder){
        try {
            if (youfanOrder.getPageNum() == null) {
                youfanOrder.setPageNum(1);
            }
            if (youfanOrder.getPageSize() == null) {
                youfanOrder.setPageSize(5);
            }
            CommonPage<YoufanOrder> resultdata =  youfanOrderService.queryByCondtion(youfanOrder);

            return Result.success(resultdata, "查询成功");
        }catch (Exception e){
            e.printStackTrace();
            return Result.fail("查询失败"+e.getMessage());
        }
    }


    @RequestMapping(method = RequestMethod.POST,value = "view")
    @ResponseBody
    public Result view(@RequestBody YoufanOrder youfanOrder)
    {
        try {
            YoufanOrder youfanOrder1 = youfanOrderService.findByid(youfanOrder);
            return Result.success(youfanOrder1,"查询单个对象成功");
        }catch (Exception e){
            e.printStackTrace();
            return Result.fail("查询单个对象失败");
        }
    }

    @RequestMapping(method = RequestMethod.POST,value = "update")
    @ResponseBody
    public Result update(@RequestBody YoufanOrder youfanOrder)
    {
        try {

            YoufanOrder youfanOrder1 = youfanOrderService.findByid(youfanOrder);
            CopyUtils.copyProperties(youfanOrder,youfanOrder1);
            int result = youfanOrderService.updateOne(youfanOrder1);
            return Result.success(result,"更新成功");
        }catch (Exception e){
            e.printStackTrace();
            return Result.fail("更新失败");
        }
    }


}
